package com.young.customer.listener;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.young.customer.service.CustomerRegisteredService;
import com.young.customer.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * @anthor wenguoqiang
 * @date: 2019/01/16 11:47
 * @company
 * @des
 **/
@Component
public class GyBalanceListener implements MessageListener {

    private static final Logger logger = LoggerFactory.getLogger(GyBalanceListener.class);
//    @Autowired
//    private OperBalanceService operBalanceService;
    @Autowired
    private CustomerRegisteredService customerRegisteredService;

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {

        //Topic校验Tag校验
        if (!message.getTopic().equals(GyBalanceMQConfig.CUSTOMER_TOPIC) || StringUtils.isEmpty(message.getTag())) {
            logger.error("Topic不符合订阅要求，Topic：" + message.getTopic() + "或者Tag为空，Tag：" + message.getTag());
            return Action.CommitMessage;
        }
        //获取消息重试次数
        int reconsume = message.getReconsumeTimes();
        if (reconsume == 3) {
            logger.error("该消息重复消费三次，直接标识成功");
            return Action.CommitMessage;
        }
        try {
            //解析消息体
            Map<String, Object> result = JsonUtils.jsonToMap(new String(message.getBody()));

            /**
             * 通过TAG来判断处理方式
             *
             */
            logger.info("获取消息：" + result.toString());
            boolean handleResult = false;//注册接收mq 手机号
//            //余额操作mq
//            if (GyBalanceMQConfig.BALANCE_FREEZE.equals(message.getTag())) {
//                logger.info("===========余额冻结============");
//                handleResult = operBalanceService.frozenOrderBalance(result);
//            } else if
//                    (GyBalanceMQConfig.BALANCE_UNFREEZE.equals(message.getTag())) {
//                logger.info("===========余额解冻或者核减============");
//                handleResult = operBalanceService.frozenOrderBalance(result);
//            } else
                if (GyBalanceMQConfig.MOBILE_REGISTERED.equals(message.getTag())) {
                //注册接收mq 手机号
                logger.info("===========注册接收mq 手机号============");
                handleResult = customerRegisteredService.Registered(result);
            } else if (GyBalanceMQConfig.WEICA_REGISTERED.equals(message.getTag())) {
                //注册接收mq 微信
                logger.info("===========注册接收mq 微信============");
                handleResult = customerRegisteredService.WeCatRegistered(result);
            } else if (GyBalanceMQConfig.ALI_REGISTERED.equals(message.getTag())) {
                //注册接收mq 支付宝
                logger.info("===========注册接收mq 支付宝============");
                handleResult = customerRegisteredService.AliRegistered(result);
            }

            if (!handleResult) {
                logger.error("处理失败；Tag：" + message.getTag());
            }
        } catch (Exception ee) {
            ee.printStackTrace();
            logger.info("异常信息 exception info" + ee.getMessage());
        }
        return Action.CommitMessage;
    }
}
